package me.lwn.auth.resources;

import me.lwn.auth.application.SmsManager;
import me.lwn.auth.common.HttpMsg;
import me.lwn.auth.security.utils.LocalMessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("sms")
public class SmsController {

    private final SmsManager smsManager;
    private final MessageSourceAccessor message = LocalMessageSource.getAccessor();

    public SmsController(SmsManager smsManager) {
        this.smsManager = smsManager;
    }

    /**
     * 短信登录
     *
     * @param mobile 手机号
     */
    @PostMapping("login")
    public HttpMsg<String> login(String mobile) {
        HttpMsg<String> response = HttpMsg.success(message.getMessage("SmsController.success"));
        response.setData(smsManager.loginCode(mobile));
        return response;
    }

    /**
     * 忘记密码
     *
     * @param mobile 手机号
     */
    @PostMapping("forget")
    public HttpMsg<String> forget(String mobile) {
        HttpMsg<String> response = HttpMsg.success(message.getMessage("SmsController.success"));
        response.setData(smsManager.forgetCode(mobile));
        return response;
    }
}
